package com.physics.mps.module.util;

import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * @author mcsoft
 * @since 2023/9/6
 */
public class PyUtils {


    // ~ Variable & Constant
    // ========================================================================================================

    /**
     * 空格
     */
    public static String BLANK = " ";

    /**
     * 换行符
     */
    public static String WRAP = "\n";

    public static String RETRACT(int n) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < n; i++) {
            builder.append("\t");
        }
        return builder.toString();
    }

    // ~ Public method
    // ========================================================================================================

    /**
     * 设置 import 命令
     *
     * @param fromParam   from 参数
     * @param importParam import 参数
     * @return import 命令
     */
    public static String createImportCommand(String fromParam, String importParam) {
        StringBuilder builder = new StringBuilder();

        if (StrUtil.isNotEmpty(fromParam)) {
            builder.append("from").append(BLANK).append(fromParam).append(BLANK);
        }
        if (StrUtil.isNotEmpty(importParam)) {
            builder.append("import").append(BLANK).append(importParam);
        }
        builder.append(WRAP);
        return builder.toString();
    }

    /**
     * 设置 chdir 命令
     *
     * @param path 路径
     * @return chdir 命令
     */
    public static String createChdirCommand(String path) {
        StringBuilder builder = new StringBuilder();
        builder.append("os.chdir( r\"")
                .append(path)
                .append("\")")
                .append(PyUtils.WRAP);
        return builder.toString();
    }


    public static String coordinateConvertToString(List<String[]> coordinates) {
        String[] co = coordinates.get(0);
        StringBuilder builder = new StringBuilder();
        builder.append("(");
        builder.append("(")
                .append(co[0]).append(",")
                .append(co[1]).append(",")
                .append(co[2]).append("), ")
                .append(")");
//        for (float[] coordinate : coordinates) {
//            builder.append("(")
//                    .append(coordinate[0]).append(",")
//                    .append(coordinate[1]).append(",")
//                    .append(coordinate[2]).append("), ");
//        }
        return builder.toString();
    }

    public static String materialConvertToString(List<List<String>> materialValues) {
        StringBuilder builder = new StringBuilder();
        for (List<String> materialValue : materialValues) {
            builder.append("(");
            for (String value : materialValue) {
                builder.append(value).append(",");
            }
            builder.deleteCharAt(builder.length() - 1).append("),");
        }
        if (materialValues.size() > 1) builder.deleteCharAt(builder.length() - 1);
        return builder.toString();
    }

    // ~ Override method
    // ========================================================================================================


    // ~ Private method
    // ========================================================================================================


    // ~ Autowired
    // ========================================================================================================


}
